<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>002 逆转字符串</title>
</head>
<body>

  <h1>随机字符串反转</h1>
  <p>让随机字符串反转，如 abc 变为 cba</p>

  <form id="myform" method="get">
    <input type="text" name="str" value="" placeholder="请随机输入字符串...">
    <input type="submit" value="反转">
  </form>
  <div id="show">loading...</div>

  <script>
    document.getElementById("myform").addEventListener("submit", function(e) {

      //阻止默认提交行为.
      e.preventDefault();

      var show = document.getElementById("show")
      var myform = document.getElementById("myform");
      var myString = myform.str.value;
      var resultString = "";

      if( !myString.length ) {
        alert("请随机输入一些字符串！");
        return false;
      }

      resultString = myString.split("").reverse().join("");

      show.innerHTML = resultString;
    }, false);
  </script>

</body>
</html>
